package com.bwf.lgqingdan.mvp.presenter.impl;

import com.bwf.lgqingdan.entity.ResponseCategoryThing;
import com.bwf.lgqingdan.entity.ResponseThings;
import com.bwf.lgqingdan.mvp.model.CategoryThingModel;
import com.bwf.lgqingdan.mvp.model.impl.CategoryThingModelImpl;
import com.bwf.lgqingdan.mvp.presenter.CategoryThingPresenter;
import com.bwf.lgqingdan.mvp.view.CategoryThingView;
import com.bwf.lgqingdan.utils.Apis;
import com.bwf.lgqingdan.utils.UrlHandler;

/**
 * Created by Administrator on 2016/11/15.
 */

public class CategoryThingPresenterImpl implements CategoryThingPresenter, CategoryThingModel.Callback {
    private CategoryThingView view;
    private CategoryThingModel model;
    private static final int TAG_NEWS = 0;
    private static final int TAG_Hottest = 1;
    private static final int TAG_Price_ASC = 2;
    private static final int TAG_Price_DESC = 3;
    private int tag;
    private int nextPage = 1;
    private int categoryId;

    public CategoryThingPresenterImpl(int categoryId, CategoryThingView view) {
        this.view = view;
        this.categoryId = categoryId;
        this.model = new CategoryThingModelImpl(categoryId);
    }

    @Override
    public void loadInitData() {
        model.loadInitData(this);
    }

    @Override
    public void loadNextPageNewesTagDetailsThings() {
        if (tag != TAG_NEWS) {
            nextPage = 1;
        }
        String url = UrlHandler.handlUrl(Apis.API_TAG_THING_NEWEST, categoryId, nextPage);
        model.loadThingsData(url, this);
    }

    @Override
    public void loadNextPageHottestTagDetailsThings() {
        if (tag != TAG_Hottest) {
            nextPage = 1;
        }
        String url = UrlHandler.handlUrl(Apis.API_TAG_THING_HOTTEST, categoryId, nextPage);
        model.loadThingsData(url, this);
    }

    @Override
    public void loadNextPagePriceAscTagDetailsThings() {
        if (tag != TAG_Price_ASC) {
            nextPage = 1;
        }
        String url = UrlHandler.handlUrl(Apis.API_TAG_THING_PRICE_ASC, categoryId, nextPage);
        model.loadThingsData(url, this);
    }

    @Override
    public void loadNextPagePriceDescTagDetailsThings() {
        if (tag != TAG_Price_DESC) {
            nextPage = 1;
        }
        String url = UrlHandler.handlUrl(Apis.API_TAG_THING_PRICE_DESC, categoryId, nextPage);
        model.loadThingsData(url, this);
    }

    @Override
    public void loadNextPageThings() {
        switch (tag) {
            case TAG_NEWS:
                loadNextPageNewesTagDetailsThings();
                break;
            case TAG_Hottest:
                loadNextPageHottestTagDetailsThings();
                break;
            case TAG_Price_ASC:
                loadNextPagePriceAscTagDetailsThings();
                break;
            case TAG_Price_DESC:
                loadNextPagePriceDescTagDetailsThings();
                break;
        }
    }

    @Override
    public void loadInitDataSuccess(ResponseCategoryThing responseCategoryThing) {
        view.showTagDetailsRelatedTags(responseCategoryThing.getData().getTagDetailsRelatedTags().getBody().getData());
        view.showTagDetailsArticles(responseCategoryThing.getData().getTagDetailsArticles().getBody().getData().getArticles());
        view.showInitThings(responseCategoryThing.getData().getTagDetailsThings().getBody().getData().getThings());
        nextPage = 2;
        tag = TAG_NEWS;
    }

    @Override
    public void loadThingsDataSuccess(ResponseThings responseThings) {
        if (responseThings.getData().getMeta().getPagination().getCurrent_page() ==
                responseThings.getData().getMeta().getPagination().getTotal_pages()) {
            view.showNoMoreThings();
        }
        view.showThings(nextPage, responseThings.getData().getThings());
        nextPage++;
    }

    @Override
    public void loadInitDataFailed(String failedMsg) {
        view.showLoadInitDataFailed(failedMsg);
    }

    @Override
    public void loadThingsDataFailed(String failedMsg) {
        view.showLoadNextThingsFaied(failedMsg);
    }
}
